﻿using System.Drawing;
using System.IO;
using System.Text.RegularExpressions;

namespace Crop;

internal class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine("输入文件夹路径");
        string inputFolder = Console.ReadLine(); // 输入文件夹路径
        Console.WriteLine("输出文件夹路径");
        string outputFolder = Console.ReadLine(); // 输出文件夹路径

        if (!Directory.Exists(outputFolder))
        {
            Directory.CreateDirectory(outputFolder);
        }

        int topCrop = 60;
        int bottomCrop = 150;

        var files = Directory.GetFiles(inputFolder, "screenshot*.png");
        for (int i = 0; i < files.Length; i++)
        {
            string inputFile = files[i];
            string fileName = Path.GetFileNameWithoutExtension(inputFile);
            string numberPart = Regex.Match(fileName, @"\d+").Value;
            string outputFile = Path.Combine(outputFolder, $"crop{numberPart}.png");

            using (Image image = Image.FromFile(inputFile))
            {
                int newHeight = image.Height - topCrop - bottomCrop;
                Rectangle cropArea = new Rectangle(0, topCrop, image.Width, newHeight);

                using (Bitmap bitmap = new Bitmap(cropArea.Width, cropArea.Height))
                {
                    using (Graphics g = Graphics.FromImage(bitmap))
                    {
                        g.DrawImage(image, new Rectangle(0, 0, bitmap.Width, bitmap.Height), cropArea, GraphicsUnit.Pixel);
                    }
                    bitmap.Save(outputFile);
                }
            }
            Console.WriteLine($"crop{numberPart}");
        }

        Console.WriteLine("批量裁剪完成！");
    }
}
